package com.dingwei.superiordiary.mvp.presenter;

import android.text.TextUtils;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.LoginBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.RegexUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URL;

/**
 * Created by lihao on 2017/9/12.
 */

public class LoginPresenter extends basePresenter {

    private Contract.LoginView loginView;

    public LoginPresenter(Contract.LoginView view) {
        super(view);
        this.loginView = view;
    }

    public void login(final String mobile, String password) {
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileExact(mobile)) {
            showToast("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        RequestParams rp = new RequestParams(URLS.HOST + URLS.LOGIN);
        rp.addBodyParameter("mobile", mobile);
        rp.addBodyParameter("password", password);
        x.http().post(rp, new myCallback(BaseActivity.context, LoginBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                LoginBean bean = (LoginBean) baseBean;
                BaseActivity.value.setIsLogin(true);
                BaseActivity.value.setPhone(mobile);
                BaseActivity.value.setUserId(bean.data.id);
                BaseActivity.value.setUserName(bean.data.name);
                BaseActivity.value.setToken(bean.data.sessionid);
                BaseActivity.value.setJPush(bean.data.jpush_code);
                loginView.loginSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void WXlogin(final String type, final String code, final String name) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.QUICK_LOGIN);
        rp.addBodyParameter("type", type);
        rp.addBodyParameter("code", code);
        x.http().post(rp, new myCallback(BaseActivity.context, LoginBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                LoginBean bean = (LoginBean) baseBean;
                BaseActivity.value.setIsLogin(true);
                BaseActivity.value.setUserName(bean.data.name);
                BaseActivity.value.setToken(bean.data.sessionid);
                BaseActivity.value.setJPush(bean.data.jpush_code);
                loginView.loginSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                if (baseBean.data.error_code.equals("00002")){
                    loginView.bindPhone(name,type,code);
                }
            }
        });
    }
}
